/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yeedal.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Neo
 */
@XmlRootElement
public enum Language {
//ARABIC          LocaleId constant for "ar".
//CHINA_CHINESE   LocaleId constant for "zh-cn".
//EMPTY           An empty locale.
//ENGLISH         LocaleId constant for "en".
//FRENCH          LocaleId constant for "fr".
//GERMAN          LocaleId constant for "de".
//ITALIAN         LocaleId constant for "it".
//JAPANESE        LocaleId constant for "ja".
//PORTUGUESE      LocaleId constant for "pt".
//RUSSIAN         LocaleId constant for "ru".
//SPANISH         LocaleId constant for "es".
//TAIWAN_CHINESE  LocaleId constant for "zh-tw".

    ARABIC("Arabic", "ar"),
    CHINESE_CHINA("简体中文", "zh_CN"),
    EMPTY(null, null),
    ENGLISH("English", "en"),
    ENGLISH_US("English US", "en_US"),
    CHINESE_TAIWAN("繁体中文", "zh_TW");
    String m_name;
    String m_id;
    private final static HashMap<Language, List<Language>> compatibleLanguages;

    static {
        compatibleLanguages = new HashMap<Language, List<Language>>();
        for (Language lang1 : getAllSupportedLanguage()) {
            List<Language> compat = new ArrayList<Language>();
            int n1 = lang1.m_id.indexOf('-');
            if (n1 == -1) {
                n1 = lang1.m_id.length();
            }
            String langCode1 = lang1.m_id.substring(0, n1);
            for (Language lang2 : getAllSupportedLanguage()) {
                int n2 = lang2.m_id.indexOf('-');
                if (n2 == -1) {
                    n2 = lang2.m_id.length();
                }
                String langCode2 = lang2.m_id.substring(0, n2);
                if (langCode1.equals(langCode2)) {
                    compat.add(lang2);
                }
            }
            compatibleLanguages.put(lang1, compat);
        }
    }

    private Language(String name, String id) {
        m_name = name;
        m_id = id;
    }

    public String getName() {
        return m_name;
    }

    public String getId() {
        return m_id;
    }

    public static List<Language> getAllSupportedLanguage() {
        List<Language> list = new ArrayList<Language>();
        list.add(CHINESE_CHINA);
        list.add(ENGLISH);
        list.add(ENGLISH_US);
        return list;
    }

    public static Language parse(String id) {
        if (null == id) {
            return EMPTY;
        }
        for (Language lang : Language.getAllSupportedLanguage()) {
            if (0 == lang.getId().compareToIgnoreCase(id)) {
                return lang;
            }
        }
        return EMPTY;
    }

    public static List<Language> getCompatibleLanguages(Language lang) {
        return compatibleLanguages.get(lang);
    }

    @Override
    public String toString() {
        return getName();
    }
}
